﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Alvin.Window.Utils
{
    /// <summary>
    /// KeyBoardWindow.xaml 的交互逻辑
    /// </summary>
    public partial class KeyBoardWindow :System.Windows. Window
    {
        private UIElement _Source = null;
        public KeyBoardWindow(UIElement source)
        {
            this._Source = source;
            InitializeComponent();
            //var handle = new WindowInteropHelper(this).Handle;
            //Test(handle);
        }
        
        public const int GWL_EXSTYLE = -20;
        public const int WS_EX_NOACTIVATE = 0x8000000;

        [DllImport("user32.dll")]
        public static extern int SetWindowLong(IntPtr window, int index, int value);

        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr window, int index);

        static void Test(IntPtr hwnd)
        {
            SetWindowLong(hwnd, GWL_EXSTYLE,GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_NOACTIVATE);
        }

        #region user control

        public static bool shiftKeyDown = false;

        private void Key_Click(object sender, RoutedEventArgs e)
        {
            this._Source.Focus();
            //RaseKey(Key.A);
            //this.Visibility = Visibility.Hidden;
            string name = ((System.Windows.FrameworkElement)(sender)).Name;
            if (shiftKeyDown)
            {
                KeyboardSend.PushTeamKeys(this.VK_SHIFT.Name, name);
                this.ChangedToSymbols(false);
                shiftKeyDown = false;
                this.VK_SHIFT.Opacity = 1;
            }
            else
            {
                KeyboardSend.PushKey(name);
                if (name.Equals(this.VK_SHIFT.Name))
                {
                    this.ChangedToSymbols(true);
                    shiftKeyDown = true;
                    this.VK_SHIFT.Opacity = 0.4;
                }
            }
            //this.Visibility = Visibility.Visible;
            //this._Source.ReleaseMouseCapture();
        }

        /// <summary>
        /// 切换至符号键盘
        /// </summary>
        private void ChangedToSymbols(bool isChange)
        {
            if (isChange)
            {
                this.Key_1.Content = "!";
                this.Key_2.Content = "@";
                this.Key_3.Content = "#";
                this.Key_4.Content = "$";
                this.Key_5.Content = "%";
                this.Key_6.Content = "^";
                this.Key_7.Content = "&";
                this.Key_8.Content = "*";
                this.Key_9.Content = "(";
                this.Key_0.Content = ")";
            }
            else
            {
                this.Key_1.Content = new TextBlock { Text = "1" };
                this.Key_2.Content = new TextBlock { Text = "2" };
                this.Key_3.Content = new TextBlock { Text = "3" };
                this.Key_4.Content = new TextBlock { Text = "4" };
                this.Key_5.Content = new TextBlock { Text = "5" };
                this.Key_6.Content = new TextBlock { Text = "6" };
                this.Key_7.Content = new TextBlock { Text = "7" };
                this.Key_8.Content = new TextBlock { Text = "8" };
                this.Key_9.Content = new TextBlock { Text = "9" };
                this.Key_0.Content = new TextBlock { Text = "0" };
            }
        }
        #endregion

        public void RaseKey(Key key)
        {
            if (_Source==null)
            {
                return;
            }
            KeyEventArgs keyEventArgs = new KeyEventArgs(System.Windows.Input.Keyboard.PrimaryDevice, System.Windows.Input.Keyboard.PrimaryDevice.ActiveSource, 0, key);
            keyEventArgs.RoutedEvent = UIElement.KeyDownEvent;
            _Source.RaiseEvent(keyEventArgs);
        }

        //protected override void OnSourceInitialized(EventArgs e)
        //{
        //    base.OnSourceInitialized(e);
        //    var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
        //    if (hwndSource == null) return;
        //    //var handle = hwndSource.Handle;
        //    hwndSource.AddHook(new HwndSourceHook(WndProc));
        //    //MessageHandle.ChangeWindowMessageFilter(PhoneEventManager.Instance.PhoneMessage, 1);
        //}

        //IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        //{
        //    if (msg == MessageHandle.NotifyRegister)
        //    {
        //        //var cds = (MessageData)Marshal.PtrToStructure(lParam, typeof(MessageData));//从发送方接收到的数据结构
        //        //var param = cds.lpData;//获取发送方传过来的消息
        //        Regist();
        //    }
        //    else
        //    {
        //        if (PhoneEventManager.Instance.IsPhoneEvent(msg))
        //        {
        //            string info;
        //            PhoneEventManager.Instance.HandleMessage(msg, wParam, lParam, ref handled, out info);
        //        }
        //    }
        //    handled = true;
        //    return hwnd;
        //}
    }
}
